﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace NetFrameworkExtensions.Threading
{
  public static class ConcurrencyHelper
  {
    public static void ThreadSafeChange<T>(ref T variable, Func<T, T> func)
      where T: class
    {
      var oldValue = variable;
      T newValue;
      do
      {
        newValue = oldValue;
        var changedValue = func(oldValue);
        oldValue = Interlocked.CompareExchange(ref variable, changedValue, oldValue);
      } while (oldValue == newValue);
    }
  }
}
